using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Jonathan.Classes;

namespace Jonathan
{
    public partial class ctrlRandomVerse : UserControl
    {
        private string m_Text;
        private string m_Location;
        private string m_Version;
        private FrmMain m_FormMain;
        private string m_Book;
        private string m_Chapter;
        private string m_Verse;

        public ctrlRandomVerse()
        {
            InitializeComponent();

            this.m_Location = "";
            this.m_Text = "";
            this.m_Version = "";
            this.m_Verse = "";
            this.m_Chapter = "";
            this.m_Book = "";

            // Set Custom Paint Styles...
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
        }

        /// <summary>
        /// Gets or Sets the Main Form...
        /// </summary>
        public FrmMain FormMain
        {
            get { return this.m_FormMain; }
            set { this.m_FormMain = value; }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            GraphicsPath p = Common.RoundRect(new RectangleF(this.ClientRectangle.X + 10,
                                                             this.ClientRectangle.Y + 10,
                                                             this.ClientRectangle.Width - 20,
                                                             this.ClientRectangle.Height - 20), 8, 8, 8, 8);

            LinearGradientBrush lgb = new LinearGradientBrush(new RectangleF(this.ClientRectangle.X + 10,
                                                              this.ClientRectangle.Y + 10,
                                                              this.ClientRectangle.Width - 20,
                                                              this.ClientRectangle.Height - 20), Color.White, Color.LightGray, 
                                                              LinearGradientMode.ForwardDiagonal);

            e.Graphics.FillPath(lgb, p);

            e.Graphics.DrawPath(new Pen(Color.LightGray), p);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            System.Reflection.Assembly assem;
            assem = this.GetType().Assembly;
            System.Resources.ResourceManager mngr = new System.Resources.ResourceManager("Jonathan.InternalImages", assem);
            Image randVerse = (Image)mngr.GetObject("RandomVerse");

            if (randVerse.Width > this.ClientRectangle.Width - 40)
            {
                int w = this.ClientRectangle.Width - 40;
                if (w > 0)
                {
                    e.Graphics.DrawImage(randVerse, new Rectangle(25, 15, w, randVerse.Height));
                }
            }
            else
            {
                e.Graphics.DrawImage(randVerse, new Point(25, 15));
            }
            this.btnGotoVerse.Top = this.Height - 100;
            this.btnGotoVerse.Left = this.Width - this.btnGotoVerse.Width - 20;

            StringFormat fmt = new StringFormat();
            fmt.Trimming = StringTrimming.EllipsisWord;
            fmt.FormatFlags = StringFormatFlags.LineLimit;
            int h = TextRenderer.MeasureText("Tg", this.Font).Height;
            
            // Draw Text...
            e.Graphics.DrawString(this.m_Text, this.Font, Brushes.Black, new RectangleF(25, 20 + randVerse.Height,
                this.ClientRectangle.Width - 40, this.btnGotoVerse.Top - (h * 7) - 25), fmt);

            Font f = new Font(this.Font.FontFamily, this.Font.Size - 3);
            // Draw Source...
            e.Graphics.DrawString("Source | " + this.m_Version, f, Brushes.Black, new RectangleF(25, 
                Convert.ToSingle(this.btnGotoVerse.Top - h * 2), this.ClientRectangle.Width - 40, h), fmt);

            fmt.Alignment = StringAlignment.Far;
            // Draw location...
            e.Graphics.DrawString(this.m_Location, this.Font, Brushes.Black, new RectangleF(25, this.btnGotoVerse.Top - (h * 4),
                this.ClientRectangle.Width - 40, h), fmt);
        }

        public void SetVerse(string text, string loc, string version, string book, string chapter, string verse)
        {
            this.m_Version = version;
            this.m_Text = text.Replace("<sup>", "(").Replace("</sup>", ")").Replace("<a href=\"\">", "").Replace("</a>", "");
            this.m_Location = loc;
            this.m_Book = book;
            this.m_Chapter = chapter;
            this.m_Verse = verse;

            this.Invalidate();
        }

        private void btnGotoVerse_Click (object sender, EventArgs e)
        {
            CPassage passage = new CPassage(this.m_Book, this.m_Chapter, this.m_Verse);
            passage.Version = this.m_Version;
            List<CPassage> passages = new List<CPassage>();
            passages.Add(passage);
            this.m_FormMain.StartBible(passages, null);
        }
    }
}
